package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.ShieldAdapter;
import com.wbao.dianniu.data.ShieldData;
import com.wbao.dianniu.listener.IBlackAccountListener;
import com.wbao.dianniu.listener.IMyBlackListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.BlackAccountManager;
import com.wbao.dianniu.manager.MyBlackListManager;
import com.wbao.dianniu.R;

/**
 * 我的黑名单
 *
 * @author yejianjun
 */
public class MyBlacklistActivity extends BaseActivity implements IMyBlackListListener, IBlackAccountListener {

    private ListView mListView;
    private List<ShieldData> mList;
    private ShieldAdapter adapter;
    private MyBlackListManager manager;
    private BlackAccountManager baManager;
    private Dialog dialog;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.my_shield);
        setTitleName("我的黑名单");
        init();
        manager = new MyBlackListManager(this);
        manager.addMyBlackListener(this);
        manager.blackList(GlobalContext.getAccountId(), 0, 999);
        baManager = new BlackAccountManager(this);
        baManager.addBlackAccountListener(this);
    }

    private void init() {
        mListView = findViewById(R.id.shield_listview);
        TextView tv = findViewById(R.id.shield_remark);
        tv.setText(getResources().getString(R.string.blackList_remark));
        adapter = new ShieldAdapter(this);
        mListView.setAdapter(adapter);

        mListView.setOnItemLongClickListener(longClickListener);
    }

    @Override
    public void onMyBlackListFailure(int errorCode, String errorMsg) {
    }

    @Override
    public void onMyBlackListSuccess(List<ShieldData> data) {
        if (null == data) {
            mList = new ArrayList<>();
        } else {
            mList = data;
            adapter.setData(mList);
            adapter.notifyDataSetChanged();
        }
    }

    OnItemLongClickListener longClickListener = new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            MyBlacklistActivity.this.position = position;
            ShowDeletedDialog("取消拉黑该用户？", position);
            return true;
        }
    };

    private void ShowDeletedDialog(String msg, final int position) {
        dialog = new Dialog(this, R.style.custom_Dialog);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(3, 3, 3, 3);
        win.setGravity(Gravity.CENTER);
        View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
        Button okBtn = view.findViewById(R.id.custom_msg_ok);
        Button cancelBtn = view.findViewById(R.id.custom_msg_cancel);
        TextView message = view.findViewById(R.id.custom_msg_message);
        message.setText(msg);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();
        okBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                baManager.blackAccount(GlobalContext.getAccountId(), mList.get(position).getAccountId());
            }
        });
        cancelBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
    }

    @Override
    public void onBlackAccountFailure(int errorCode, String errorMsg) {
    }

    @Override
    public void onBlackAccountSuccess() {
        // TODO Auto-generated method stub
        mList.remove(position);
        adapter.notifyDataSetChanged();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeMyBlackListener(this);
        }
        if (null != baManager) {
            baManager.removeBlackAccountListener(this);
        }
    }
}
